/**
 * 公共变量
 */
var Picture = {};

/**
 * 页面初始化
 */
$(function () {
    //判断是在图片管理还是在冲泡方式页面中打开
    if ($('#openType').val() == 'brewsAdd' || $('#openType').val() == 'brewsEdit') {
        //初始化图片选择
        selectImgTake.init('selectItemDiv', 1);
        //获得编辑的步骤
        Picture.sequence = $('#sequence').val();
        Picture.label = "";
    } else if ($('#openType').val() == 'teaAdd' || $('#openType').val() == 'teaEdit') {
        selectImgTake.init('selectItemDiv', 5);
        Picture.label = $('#label').val();
        Picture.sequence = "";
    } else {
        selectImgTake.init('selectItemDiv', 8);
    }
    //获得编辑的条目id
    Picture.itemId = $("#itemId").val();

    //初始化分页插件
    loadpage();

    //鼠标悬停放大图片
    showImg();
});

/**
 * 鼠标悬停放大图片
 */
function showImg() {
    var x = 10;
    var y = 20;
    $("img").mouseover(function (e) {
        //创建 div 元素
        var tooltip = "<div id='tooltip'><img src='" + this.src + "' alt='预览图'/><\/div>";
        $("body").append(tooltip);	//把它追加到文档中
        $("#tooltip")
            .css({
                "top": (e.pageY + y) + "px",
                "left": (e.pageX + x) + "px"//设置x坐标和y坐标，并且显示
            }).show("fast");
    }).mouseout(function () {
        $("#tooltip").remove();
    }).mousemove(function (e) {
        $("#tooltip")
            .css({
                "top": (e.pageY + y) + "px",
                "left": (e.pageX + x) + "px"
            });
    });
}

/**
 * 判断是否选择了图片
 */
Picture.check = function () {
    //获得选中图片
    var selectImgDivs = selectImgTake.getSelectImgs("selectImgDiv");
    if (selectImgDivs.length < 1) {
        Feng.info("请选择一张图片！");
        return false;
    } else {
        Picture.selectImgDivs = selectImgDivs;
        return true;
    }
}

/**
 * 确定选择关联图片
 */
Picture.selector = function () {
    //判断是新增页面还是编辑页面
    var operation;
    var selectImgDivs = selectImgTake.getSelectImgs("selectImgDiv");
    Picture.selectImgDivs = selectImgDivs;
    //if (Picture.itemId) {
    if (false) {
        //编辑界面
        operation = function () {
            //关联图片之前先删除之前的图片关联关系
            Picture.cancelSelector(Picture.sequence, Picture.label);
            if (Picture.selectImgDivs.length < 1) {
                Feng.success("重置图片关联成功!");
            } else {
                var postUrl;
                if (Picture.label) {
                    postUrl = Feng.ctxPath + "/teaFile/selector";
                    //Picture.sequence =
                } else {
                    postUrl = Feng.ctxPath + "/brews/selector";
                }
                var ajax = new $ax(postUrl, function (data) {
                    Feng.success("关联图片成功!");
                }, function (data) {
                    Feng.error("关联图片失败!" + data.responseJSON.message + "!");
                });
                //冲泡方式的id
                ajax.set("itemId", Picture.itemId);
                //拼接选中的图片id（用小横杠来拼接）Picture.selectImgDivs[i].id
                for (var i = 1, idValue = Picture.selectImgDivs[0].id; i < Picture.selectImgDivs.length; i++) {
                    if (idValue.endsWith("-")) {
                        idValue = idValue + Picture.selectImgDivs[i].id;
                    } else {
                        idValue = idValue + "-" + Picture.selectImgDivs[i].id;
                    }
                    if (i != Picture.selectImgDivs.length - 1) {
                        idValue = idValue + "-";
                    }
                }
                ajax.set("label", Picture.label);
                ajax.set("sequence", Picture.sequence);
                ajax.set("ids", idValue);
                ajax.start();
            }
            Picture.showSelectPicture();
            //关闭窗口
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        };
    } else {
        //新增界面则将图片临时放到新增页面中
        operation = function () {
            Picture.showSelectPicture();
        };
    }
    operation();
    // Feng.confirm("是否重新关联图片？", operation);
};

/**
 * 将图片显示到上层页面中
 */
Picture.showSelectPicture = function () {
    //判断是茶叶界面还是冲泡方式界面
    if ($('#openType').val().indexOf('tea') > -1) {
        if (Picture.label == '茶叶介绍') {
            Picture.imgBeginNum = 0;
        } else if (Picture.label == '品牌介绍') {
            Picture.imgBeginNum = 5;
        } else if (Picture.label == '产品包装') {
            Picture.imgBeginNum = 10;
        } else {
            Feng.error('Picture.label没有对应的常量值');
            return;
        }
        //先清空原有的显示图片
        for (var i = 0; i < 5; i++) {
            Picture.cancelShowOnePicture(Picture.imgBeginNum + i);
        }
        //将选择的图片展示到界面中
        for (var i = 0; i < Picture.selectImgDivs.length; i++) {
            Picture.showOnePicture(Picture.imgBeginNum + i);
        }
    } else {
        //冲泡方式界面
        if (Picture.selectImgDivs.length > 0) {
            Picture.showOnePicture(Picture.sequence, 'brews');
        } else {
            //清空一张
            Picture.cancelShowOnePicture(Picture.sequence);
        }
    }
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
};

/**
 * 显示一张
 * @param i
 */
Picture.showOnePicture = function (i, type) {
    if (!Picture.imgBeginNum) {
        Picture.imgBeginNum = 0;
    }
    var selOrder;
    if (type == 'brews') {
        selOrder = 0;
    } else {
        selOrder = i - Picture.imgBeginNum;
    }
    window.parent.$('#img' + i).attr('selId', Picture.selectImgDivs[selOrder].id);
    window.parent.$('#img' + i).attr('src', Picture.selectImgDivs[selOrder].getAttribute('selSrc'));
    window.parent.$('#img' + i).css('display', 'block');

};

/**
 * 取消显示一张
 * @param i
 */
Picture.cancelShowOnePicture = function (i) {
    window.parent.$('#img' + i).attr('selId', "");
    window.parent.$('#img' + i).attr('src', "");
    window.parent.$('#img' + i).css('display', 'none');
};

/**
 * 查询
 */
Picture.search = function () {
    loadData(Feng.getUrlParam("currentPage"), $("#condition").val(), $("#beginTime").val(), $("#endTime").val());
};

/**
 * 重置查询
 */
Picture.resetSearch = function () {
    loadData();
};

/**
 * 重新初始化分页插件
 */
function loadpage() {
    //总记录数
    var myTotal = parseInt($("#total").val());
    //每页的记录数
    var myPageSize = parseInt($("#PageSize").val());
    //计算出的页数
    var countindex = myTotal % myPageSize > 0 ? (myTotal / myPageSize) + 1 : (myTotal / myPageSize);
    $("#countindex").val(countindex);
    //当前的页面数
    var currentPage = Feng.getUrlParam("currentPage");
    if (!currentPage) {
        currentPage = 1;
    }

    $.jqPaginator('#pagination', {
        totalPages: parseInt($("#countindex").val()),
        visiblePages: parseInt($("#visiblePages").val()),
        currentPage: parseInt(currentPage),
        first: '<li class="first"><a href="javascript:;">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>上一页</a></li>',
        next: '<li class="next"><a href="javascript:;">下一页<i class="arrow arrow3"></i></a></li>',
        last: '<li class="last"><a href="javascript:;">末页</a></li>',
        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
        onPageChange: function (num, type) {
            if (type == "change") {
                //点击分页查询
                loadData(num);
            }
        }
    });
}

/**
 * 点击分页查询
 * @param num 页码
 */
function loadData(num, condition, beginTime, endTime) {
    if (!num) {
        num = 1;
    }
    if (!condition) {
        condition = "";
    }
    if (!beginTime) {
        beginTime = "";
    }
    if (!endTime) {
        endTime = "";
    }
    var limit = parseInt($("#PageSize").val());
    var offset = (num - 1) * parseInt($("#PageSize").val());
    //判断是在茶叶界面打开还是冲泡方式界面中打开
    if ($('#openType').val().indexOf('tea') > -1) {
        window.location.href = Feng.ctxPath + "/teaFile/tea_picture?limit=" + limit
            + "&offset=" + offset
            + "&currentPage=" + num
            + "&condition=" + condition
            + "&beginTime=" + beginTime
            + "&endTime=" + endTime
            + "&label=" + Picture.label
            + "&teaId=";
    } else if ($('#openType').val().indexOf('brews') > -1) {
        window.location.href = Feng.ctxPath + "/brews/brews_picture?limit=" + limit
            + "&offset=" + offset
            + "&currentPage=" + num
            + "&condition=" + condition
            + "&beginTime=" + beginTime
            + "&endTime=" + endTime
            + "&sequence=" + Picture.sequence
            + "&brewsId=";
    }
}



